package com.whpu.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whpu.common.utils.PageUtils;
import com.whpu.common.utils.Query;
import com.whpu.mall.product.dao.BrandDao;
import com.whpu.mall.product.entity.BrandEntity;
import com.whpu.mall.product.service.BrandService;
import com.whpu.mall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    private CategoryBrandRelationService relationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //模糊查询
        //获取key
        String key = (String) params.get("key");
        //添加检索条件
        QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)) {
            wrapper.eq("brand_id", key).or().like("name", key);

        }

        IPage<BrandEntity> page = this.page(new Query<BrandEntity>().getPage(params), wrapper);


        return new PageUtils(page);
    }

    /**
     * 级联更新
     * @param brand
     */
    @Override
    public void updateCasc(BrandEntity brand) {
        //保证冗余字段的一致性,第三张表brandId和catelogId关联,name是为了防止连表查询影响效率添加的冗余字段
        this.updateById(brand); //修改本身的表

        if (!StringUtils.isEmpty(brand.getName())){
            //同步更新其他关联表中的数据,根据id更改name
            relationService.updateBrand(brand.getBrandId(),brand.getName());

            //TODO  更新其他关联

        }

    }

}
